{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Investigate Model Behavior"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# MODULE IMPORTS ----\n",
    "import hddm"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "From version HDDM >= 0.9.0, you have access to multiple **new sequential sampling models**. \n",
    "You can simulate from these models, perform parameter estimation and moreover you have some extended plotting capabilities which can be useful to visualize model fits, or simply to investigate the behavior of models across parameter settings."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Metadata"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Lets take a look at the new `hddm.model_config.model_config` dictionary, which allows you to investigate metadata for all the new (and old) models which are available through the HDDM-LAN extension. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['ddm_hddm_base',\n",
       " 'full_ddm_hddm_base',\n",
       " 'ddm',\n",
       " 'angle',\n",
       " 'weibull',\n",
       " 'levy',\n",
       " 'full_ddm',\n",
       " 'ornstein',\n",
       " 'ddm_sdv',\n",
       " 'ddm_par2']"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# List all available models\n",
    "list(hddm.model_config.model_config.keys())[:10]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'doc': 'Model formulation is described in the documentation under LAN Extension.Meant for use with the extension.',\n",
       " 'params': ['v', 'a', 'z', 'g', 't'],\n",
       " 'params_trans': [0, 0, 1, 0, 0],\n",
       " 'params_std_upper': [1.5, 1.0, None, 1.0, 1.0],\n",
       " 'param_bounds': [[-2.0, 0.3, 0.2, -1.0, 0.001], [2.0, 2.0, 0.8, 1.0, 2]],\n",
       " 'param_bounds_cnn': [[-2.5, 0.2, 0.1, -1.0, 0.0], [2.5, 2.0, 0.9, 1.0, 2.0]],\n",
       " 'boundary': <function hddm.simulators.boundary_functions.constant(t=0)>,\n",
       " 'params_default': [0.0, 1.0, 0.5, 0.0, 0.001],\n",
       " 'hddm_include': ['z', 'g'],\n",
       " 'choices': [-1, 1],\n",
       " 'slice_widths': {'v': 1.5,\n",
       "  'v_std': 0.1,\n",
       "  'a': 1,\n",
       "  'a_std': 0.1,\n",
       "  'z': 0.1,\n",
       "  'z_trans': 0.2,\n",
       "  't': 0.01,\n",
       "  't_std': 0.15,\n",
       "  'g': 0.1,\n",
       "  'g_trans': 0.2,\n",
       "  'g_std': 0.1}}"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Take an example to list data available for a given model\n",
    "model_tmp = \"ornstein\"\n",
    "hddm.model_config.model_config[model_tmp]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You have access to the following data (we focus on the parts important for the user):\n",
    "\n",
    "- `params`, the names of paramaters for a given model (order matters)\n",
    "- `params_trans` whether HDDM should internally transform a parameter to an unconstrained domain\n",
    "- `param_bounds` the range of parameter values that the respective LAN was trained on (order as in `params`) \n",
    "- `boundary` the boundary function, which corresponds to the model (access the available boundary functions through the `hddm.simulators.boundary_functions` module.\n",
    "- `params_default`, defaults settings for the parameters of the model\n",
    "- `hddm_include`, list to supply to hddm to include all model parameters (you may want to drop some)\n",
    "-  `slide_widths`, slice sampler settings parameter by parameter (changing these can improve / deteriorate sampler behavior)\n",
    "\n",
    "You can change these settings as you see fit. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Simulate"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The new `simulator_h_c()` function lets you generate complex datasets using the models available under `hddm.model_config.model_config`. The function is especially useful for parameter recovery studies. It can generate fully synthetic data, or you can supply an empirial dataset and it's structure can be used to generate simulation based replicas. Find more information using the `help()` function. \n",
    "Here we give a simple example."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "# test regressors only False\n",
    "# add p_outliers to the generator !\n",
    "model = \"angle\"\n",
    "n_subjects = 1\n",
    "n_samples_by_subject = 500\n",
    "\n",
    "data, full_parameter_dict = hddm.simulators.hddm_dataset_generators.simulator_h_c(\n",
    "    n_subjects=n_subjects,\n",
    "    n_samples_by_subject=n_samples_by_subject,\n",
    "    model=model,\n",
    "    p_outlier=0.00,\n",
    "    conditions=None,\n",
    "    depends_on=None,\n",
    "    regression_models=None,\n",
    "    regression_covariates=None,\n",
    "    group_only_regressors=False,\n",
    "    group_only=None,\n",
    "    fixed_at_default=None,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>rt</th>\n",
       "      <th>response</th>\n",
       "      <th>subj_idx</th>\n",
       "      <th>v</th>\n",
       "      <th>a</th>\n",
       "      <th>z</th>\n",
       "      <th>t</th>\n",
       "      <th>theta</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.770753</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.380409</td>\n",
       "      <td>0.839763</td>\n",
       "      <td>0.479834</td>\n",
       "      <td>0.515753</td>\n",
       "      <td>0.959683</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.812753</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.380409</td>\n",
       "      <td>0.839763</td>\n",
       "      <td>0.479834</td>\n",
       "      <td>0.515753</td>\n",
       "      <td>0.959683</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.707753</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.380409</td>\n",
       "      <td>0.839763</td>\n",
       "      <td>0.479834</td>\n",
       "      <td>0.515753</td>\n",
       "      <td>0.959683</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.616753</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.380409</td>\n",
       "      <td>0.839763</td>\n",
       "      <td>0.479834</td>\n",
       "      <td>0.515753</td>\n",
       "      <td>0.959683</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0.746753</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.380409</td>\n",
       "      <td>0.839763</td>\n",
       "      <td>0.479834</td>\n",
       "      <td>0.515753</td>\n",
       "      <td>0.959683</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>95</th>\n",
       "      <td>0.696753</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.380409</td>\n",
       "      <td>0.839763</td>\n",
       "      <td>0.479834</td>\n",
       "      <td>0.515753</td>\n",
       "      <td>0.959683</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>96</th>\n",
       "      <td>0.778753</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.380409</td>\n",
       "      <td>0.839763</td>\n",
       "      <td>0.479834</td>\n",
       "      <td>0.515753</td>\n",
       "      <td>0.959683</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>97</th>\n",
       "      <td>0.708753</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.380409</td>\n",
       "      <td>0.839763</td>\n",
       "      <td>0.479834</td>\n",
       "      <td>0.515753</td>\n",
       "      <td>0.959683</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>98</th>\n",
       "      <td>0.848752</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.380409</td>\n",
       "      <td>0.839763</td>\n",
       "      <td>0.479834</td>\n",
       "      <td>0.515753</td>\n",
       "      <td>0.959683</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>99</th>\n",
       "      <td>0.817753</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.380409</td>\n",
       "      <td>0.839763</td>\n",
       "      <td>0.479834</td>\n",
       "      <td>0.515753</td>\n",
       "      <td>0.959683</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>100 rows × 8 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "          rt  response subj_idx         v         a         z         t  \\\n",
       "0   0.770753       1.0        0  0.380409  0.839763  0.479834  0.515753   \n",
       "1   0.812753       1.0        0  0.380409  0.839763  0.479834  0.515753   \n",
       "2   0.707753       0.0        0  0.380409  0.839763  0.479834  0.515753   \n",
       "3   0.616753       1.0        0  0.380409  0.839763  0.479834  0.515753   \n",
       "4   0.746753       1.0        0  0.380409  0.839763  0.479834  0.515753   \n",
       "..       ...       ...      ...       ...       ...       ...       ...   \n",
       "95  0.696753       0.0        0  0.380409  0.839763  0.479834  0.515753   \n",
       "96  0.778753       1.0        0  0.380409  0.839763  0.479834  0.515753   \n",
       "97  0.708753       1.0        0  0.380409  0.839763  0.479834  0.515753   \n",
       "98  0.848752       0.0        0  0.380409  0.839763  0.479834  0.515753   \n",
       "99  0.817753       1.0        0  0.380409  0.839763  0.479834  0.515753   \n",
       "\n",
       "       theta  \n",
       "0   0.959683  \n",
       "1   0.959683  \n",
       "2   0.959683  \n",
       "3   0.959683  \n",
       "4   0.959683  \n",
       "..       ...  \n",
       "95  0.959683  \n",
       "96  0.959683  \n",
       "97  0.959683  \n",
       "98  0.959683  \n",
       "99  0.959683  \n",
       "\n",
       "[100 rows x 8 columns]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# A look at the data generated\n",
    "data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The `full_parameter_dic` returned plays well with `HDDM` and some plots that give you the option\n",
    "to provide ground truth parameters. In our case the output is simple. More complicated\n",
    "datasets, will make this much more interesting.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'z': 0.4798341238342858,\n",
       " 'theta': 0.9596826258981702,\n",
       " 'v': 0.3804088421388936,\n",
       " 't': 0.51575296339607,\n",
       " 'a': 0.8397625801445228}"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "full_parameter_dict"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "hddmnn_tutorial",
   "language": "python",
   "name": "hddmnn_tutorial"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
